package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.utils.TreeModel;
import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.ISysRoleDao;
import com.yeshine.opendata.entity.SysModel;
import com.yeshine.opendata.entity.SysRole;
import com.yeshine.opendata.service.ISysRoleService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by taohj on 2014/9/3.
 */
@Service
public class SysRoleServiceImpl extends ServiceBase<SysRole> implements ISysRoleService {

    @Autowired
    private ISysRoleDao dao;

    @Override
    public Result<SysRole> create(SysRole sysRole) {
        return new Result<SysRole>(dao.insert(sysRole) > 0);
    }

    @Override
    public Result<SysRole> remove(SysRole sysRole) {
        return new Result<SysRole>(dao.delete(sysRole) > 0);
    }

    @Override
    public Result<SysRole> modify(SysRole sysRole) {
        return new Result<SysRole>(dao.update(sysRole) > 0);
    }

    @Override
    public Result<SysRole> getById(Integer id) {
        return new Result<SysRole>(dao.selectById(id));
    }

    @Override
    public Result<SysRole> getList(SysRole sysRole) {
        return new Result<SysRole>(dao.selectList(sysRole));
    }

    @Override
    public Result<SysRole> getByPage(PageResult<SysRole> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<SysRole>(pageResult);
    }

    @Override
    public Result authorization(int role, int[] models) {
     ///   dao.deauthorize(role);
        return new Result<SysRole>(dao.authorization(role,models)>0);
    }

    @Override
    public List<SysModel> getPermission(int role) {
        return dao.selectPermission(role);
    }

    @Override
    public List<TreeModel> getTreeModel(int role) {
        return null;
    }



    @Override
    public List<TreeModel> getTreeModel(List<TreeModel> allModels){
        List<TreeModel> rootModels = new ArrayList<TreeModel>();
        for (int i = 0; i < allModels.size(); i++) {
            if (allModels.get(i).getParent() == null || allModels.get(i).getParent().getId()==null) {
                if (!rootModels.contains(allModels.get(i))) {
                    rootModels.add(allModels.get(i));
                }
                getTreeChidren(allModels, allModels.get(i));
            }
        }
        return rootModels;

    }



    public TreeModel getTreeChidren(List<TreeModel> models, TreeModel model) {

        model.setChildren(model.getChildren() == null ? new ArrayList<TreeModel>() : model.getChildren());
        for (int i = 0; i < models.size(); i++) {

            TreeModel m = models.get(i);
            if (m.getParent() != null && model.getId().equals(m.getParent().getId())) {
                if (!model.getChildren().contains(m)) {
                    model.getChildren().add(m);
                }
                getTreeChidren(models, m);
            }
        }

        return model;
    }




}
